#pragma once
#include <vector>
#include <functional>
namespace ljt
{
    
    template <class T, class Container = std::vector<T>, class Compare = std::less<T>>

    class priority_queue
    {

    public:

        priority_queue()
        {

        }

        void down(int parent)
        {   
            Compare com;
            size_t child = parent * 2 + 1;
            while (child < c.size())
            {
                if (child + 1 < c.size() && com(c[child], c[child + 1]))
                    child++;

                if (com(c[parent], c[child]))
                {
                    std::swap(c[child], c[parent]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else break;
            }

            
        }

        void up(int child)
        {
            Compare com;
            size_t parent = (child - 1) / 2;
            while (child > 0)
            {
                if (com(c[parent], c[child]))
                {
                    std::swap(c[child], c[parent]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else break;
            }
        }
        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
            :c(first, last)
        {
            for (int i = 0; i < c.size() / 2; i++)
            {
                down(i);
            }
        }

        bool empty() const
        {
            return c.empty();
        }

        size_t size() const
        {
            return c.size();
        }

        T& top() 
        {
            return c[0];
        }

        void push(const T& x)
        {
            c.push_back(x);
            up(c.size() - 1);
        }

        void pop()  
        {
            std::swap(c[0], c[c.size() - 1]);
            c.pop_back();
            down(0);
        }

    private:

        Container c;

        Compare comp;

    };

};